package com.bdqn.t350.ch11.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.t350.ch11.bean.Right;
import com.bdqn.t350.ch11.bean.Role;
import com.bdqn.t350.ch11.bean.RoleRight;
import com.bdqn.t350.ch11.config.shiro.MyShiroRealm;
import com.bdqn.t350.ch11.service.RightService;
import com.bdqn.t350.ch11.service.RoleRightService;
import com.bdqn.t350.ch11.service.RoleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Resource
    RoleService roleService;

    @Resource
    RightService rightService;

    @Resource
    RoleRightService roleRightService;

    @ResponseBody
    @RequestMapping("/list")
    public Object list(Model model) {
        List<Role> roleList = roleService.selectList(null, new Role());
        //model.addAttribute("roleList", roleList);
        return roleList;
    }

    @RequestMapping("/listPage")
    public Object listPage(@RequestParam(name = "roleName", defaultValue = "") String roleName,
                           @RequestParam(name = "pageIndex", defaultValue = "1") Integer pageIndex,
                           Model model) {
        IPage<Role> page = new Page<>();
        page.setSize(5);
        page.setCurrent(pageIndex);
        Role role = new Role();
        role.setRoleName(roleName);
        List<Role> roleList = roleService.selectList(page, role);
        page.setRecords(roleList);
        model.addAttribute("roleName", roleName);
        model.addAttribute("page", page);
        return "role/list";
    }

    /**
     * 转向添加页面
     */
    @RequestMapping("/toadd")
    public String toadd(Model model) {
        return "role/add";
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public String save(Role role) {
        boolean bool = roleService.insert(role);
        if (bool) {
            return "redirect:/role/listPage";
        } else {
            throw new RuntimeException("添加数据异常");
        }
    }

    /**
     * 权限更新
     */
    @PostMapping("/auth")
    public String auth(@RequestParam(value = "auth", required = false) String auth,
                       @RequestParam(value = "roleId") Integer roleId) {
        if (auth != null) {
            //页面传递过来的权限集合
            Set addSet = new HashSet<>(Arrays.asList(auth.split(",")));
            //根据角色id查询数据库里面的权限集合
            List<String> list = roleService.getRightByRoleId(roleId);
            //目前数据库中的权限集合
            Set dbSet = new HashSet<>(list);
            //新增权限集合=页面传递过来的权限集合-目前数据库中的权限集合
            addSet.removeAll(dbSet);
            roleRightService.batchInsert(addSet, roleId);

            //删除权限集合=目前数据库中的权限集合-页面传递过来的权限集合
            dbSet.removeAll(Arrays.asList(auth.split(",")));
            roleRightService.deleteByIds(dbSet, roleId);
        } else {
            //根据角色ID删除所有角色对应的资源
            roleRightService.deleteAll(roleId);
        }
        return "redirect:/role/selectByRoleId?roleId=" + roleId;
    }

    /**
     * 根据角色id查看
     */
    @RequestMapping("/selectByRoleId")
    public String selectByRoleId(Long roleId, Model model) {
        Role role = new Role();
        role.setRoleId(roleId);
        List<Role> roles = roleService.selectByRoleId(role);
        List<String> rightList = new ArrayList<>();
        for (Role r : roles) {
            role = r;
            for (RoleRight roleRight : r.getRoleRights()) {
                rightList.add(roleRight.getRfRightCode());
            }
        }
        List<Right> rights = rightService.selectList();
        model.addAttribute("role", role);
        model.addAttribute("rights", rights);
        model.addAttribute("rightList", rightList);
        return "role/view1";
    }
}
